package com.guli.educenter.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @ClassName: MD5 
 * @Description: TODO
 * @author: liuzg
 * @Date: 2021/3/26 9:09
 * @Version: v1.0
 */
public class MD5 {

    // md5加密中参杂一些秘钥
    private static final String secretKey = "com.guli.college";

    /**
     * 加密
     *
     * @param strSrc
     * @return
     */
    public static String encrypt(String strSrc) {
        strSrc += secretKey;
        try {
            char hexChars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
                    '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
                    'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
            byte[] bytes = strSrc.getBytes();
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(bytes);
            bytes = md.digest();
            int j = bytes.length;
            char[] chars = new char[j * 2];
            int k = 0;
            for (int i = 0; i < bytes.length; i++) {
                byte b = bytes[i];
                chars[k++] = hexChars[b >>> 4 & 0xf];
                chars[k++] = hexChars[b & 0xf];
            }
            return new String(chars);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new RuntimeException("MD5加密出错！！+" + e);
        }
    }

    // 测试
    public static void main(String[] args) {
        System.out.println(MD5.encrypt("123456"));
    }
}
